using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Portfolio : System.Web.UI.Page 
{
    protected string PrevPageQS;
    protected string NextPageQS;
    protected int CategoryID;
    protected int PageNumber;
    protected double MaxPages;
    protected int MaxItems;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        CategoryID = -1;
        OleDbDataReader CategoryReader;
        PageNumber = 1;        
        const int ITEMS_PER_PAGE = 4;
        const int ITEMS_PER_ROW = 2;
        int CatID = 0;
        int Counter = 0;
        int PageNo = 0;
        string DetailQS = "";
        string Output = "";

        // determine the category id
        if (Int32.TryParse(Request.QueryString["category"], out CatID)) {
            CategoryID = CatID; }
        
        // determine the page number
        if (Int32.TryParse(Request.QueryString["page"], out PageNo)) {
            PageNumber = PageNo; }

        PrevPageQS = "?category=" + CategoryID + "&page=" + (PageNumber - 1).ToString();
        NextPageQS = "?category=" + CategoryID + "&page=" + (PageNumber + 1).ToString();
        
        // populate the category list
        CategoryReader = DAL.GetCategories();
        Output += "<a class=\"CatMenuLink\" href=\"portfolio.aspx\">";
        Output += "<span";
        if (CategoryID == -1) {
            Output += " style=\"font-weight: bold;\""; }
        Output += ">";
        Output += "All Items";
        Output += "</span>";
        Output += "</a>";
        Output += "<br/>";
       
        if (CategoryReader.HasRows) {
            while (CategoryReader.Read())
            {
                Output += "<a class=\"CatMenuLink\" href=\"portfolio.aspx?category=" + CategoryReader.GetInt32(0).ToString() + "\">";
                Output += "<span";
                if (CategoryID == CategoryReader.GetInt32(0)) {
                    Output += " style=\"font-weight: bold;\""; }
                Output += ">";
                Output += CategoryReader.GetString(1);
                Output += "</span>";
                Output += "</a>";
                Output += "<br/>";
            }        
            CategoryList.Text = Output;
            CategoryReader.Close(); }

        // populate the items list
        System.Data.DataTable ItemTable;
        if (CategoryID >= 0) {
            ItemTable = DAL.GetItems(ITEMS_PER_PAGE, PageNumber, CategoryID, out MaxItems); }
        else {
            ItemTable = DAL.GetItems(ITEMS_PER_PAGE, PageNumber, out MaxItems); }
        
        // determine the maximum number of pages
        MaxPages = (double) MaxItems / (double) ITEMS_PER_PAGE;
        
        // build the output for the item list      
        Output = "";
        Counter = 1;

        foreach (DataRow Row in ItemTable.Rows)
        {
            if (Counter == 1) {
                Output += "<tr>"; }
            
            DetailQS = "?category=" + CategoryID.ToString() + "&page=" + PageNumber.ToString() + "&item=" + Row["ID"].ToString();
            
            Output += "<td class=\"ItemCell\">";
            Output += "<a href=\"detail.aspx" + DetailQS + "\" class=\"DetailLink\">";
            Output += "<img src=\"/products/thumbnails/" + Row["Thumbnail"] + "\" class=\"ThumbnailImage shadow\">";
            Output += "<br>";
            Output += "<span class=\"ThumbnailCaption\">" + Row["Name"] + "</span>";
            Output += "</a>";
            Output += "</td>\n";

            if (Counter == ITEMS_PER_ROW) {
                Output += "</tr>\n";
                Counter = 1; }
            else {
                Counter++; }
        }
        
        // write the item list
        ItemList.Text = Output;
    }
}
